<template>
  <div class="dialog-form">
    <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
      <el-row :gutter="36">
        <el-col :span="12">
          <el-form-item label="公告标题" prop="noticeTitle">
            <el-input v-model="form.noticeTitle" placeholder="请输入公告标题" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公告类型" prop="noticeType">
            <el-select v-model="form.noticeType" placeholder="请选择">
              <el-option
                v-for="dict in sys_notice_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                  v-for="dict in sys_notice_status"
                  :key="dict.value"
                  :value="dict.value"
              >{{ dict.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="内容">
            <editor v-model="form.noticeContent" :min-height="210"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { getNotice, addNotice, updateNotice } from "@/api/system/notice"

const { proxy } = getCurrentInstance()
const { sys_notice_status, sys_notice_type } = proxy.useDict("sys_notice_status", "sys_notice_type")
const emits = defineEmits(['cancel', 'confirm'])

const noticeRef = ref()
const form = ref({
  noticeId: undefined,
  noticeTitle: undefined,
  noticeType: undefined,
  noticeContent: undefined,
  status: "0"
})
const rules = {
  noticeTitle: [{ required: true, message: "公告标题不能为空", trigger: "blur" }],
  noticeType: [{ required: true, message: "公告类型不能为空", trigger: "change" }]
}

const pageInit = (noticeId) => {
  if (noticeId) {
    getNotice(noticeId).then(response => {
      form.value = response.data
    })
  }
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  noticeRef.value.validate(valid => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          noticeRef.value.resetFields()
          emits('confirm')
        })
      } else {
        addNotice(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          noticeRef.value.resetFields()
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>